﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Xxf.IISM.Core
{
    public static class DataPageHelper
    {
        public static JqTableResult<T> Page<T>(this IQueryable<T> source, JqTableRequest request) where T : class
        {
            var data = new JqTableResult<T>();
            data.recordsTotal = source.Count();
            string orderName = request.columns[request.Order[0].Column].Data;
            data.Draw = 1;
            data.recordsFiltered = data.recordsTotal;
            data.data = source.OrderByExt(orderName, request.Order[0].Dir).Skip(request.Start).Take(request.Length).ToList();
            return data;
        }
        /// <summary>
        /// 扩展排序 字符串
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="source"></param>
        /// <param name="orderByProperty"></param>
        /// <param name="ascStr"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> OrderByExt<TEntity>(this IQueryable<TEntity> source, string orderByProperty, string ascStr) where TEntity : class
        {
            bool desc = ascStr != "asc";
            return source.OrderByExt(orderByProperty, desc);
        }
        /// <summary>
        /// 扩展排序
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="source"></param>
        /// <param name="orderByProperty"></param>
        /// <param name="desc"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> OrderByExt<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc) where TEntity : class
        {
            string command = desc ? "OrderByDescending" : "OrderBy";

            var type = typeof(TEntity);
            var property = type.GetProperty(orderByProperty);
            var parameter = Expression.Parameter(type, "p");
            var propertyAccess = Expression.MakeMemberAccess(parameter, property);
            var orderByExpression = Expression.Lambda(propertyAccess, parameter);
            var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExpression));
            return source.Provider.CreateQuery<TEntity>(resultExpression);
        }
        public static IQueryable<TEntity> Search<TEntity>(this IQueryable<TEntity> source, string propertyName, string searchValue)
        {
            var type = typeof(TEntity);
            var property = type.GetProperty(propertyName);
            var parameter = Expression.Parameter(type, "p");
            var propertyAccess = Expression.MakeMemberAccess(parameter, property);
            var searchExpression = Expression.Lambda(propertyAccess, parameter);
            var resultExpression = Expression.Call(typeof(Queryable), "Contains", new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(searchExpression));
            return source.Provider.CreateQuery<TEntity>(resultExpression);
        }
    }
}
